<?php

namespace App\Admin\Extensions;
use Dcat\Admin\Widgets\Modal;
use App\Admin\Controllers\UserController;
use App\Admin\Controllers\UserBingshiController;
use App\Admin\Controllers\UserDanganController;
use App\Admin\Controllers\UserWenjuanController;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Tab;

class AddBingshi {
    public function __construct(){

    }
    public function render()
    {
        $modal = Modal::make()
        ->title('初诊')
        ->body($this->view())
        ->xl()
        ->onShow(
    <<<JS
    $('.step-2').click(function(){
        $(this).parents('.modal-body').find('.nav-tabs li a:eq(1)').tab('show');
        window.location.hash = 'tab_bingshi-form-tab';
   })
   $('.step-1').click(function(){
        $(this).parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
        window.location.hash = 'tab_user-form-tab';
   })

   $('.saveBingli').click(function(){
        var user = {};
        $.each($('#tab_user-form-tab form ').serializeArray(), function(index, item) {
            user[item.name] = item.value;
        });
        console.log($('#tab_user-form-tab form ').serializeArray());
        console.log(user);
        if(user.mobile == '' || user.name == '' || user.school == '' || user.class_name == '' || user.sex == '') {
            $('.step-1').parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
            window.location.hash = 'tab_user-form-tab';
            $('#tab_user-form-tab form ').submit()
            Dcat.swal.error('错误', '客户信息请填写完整');
            return false
        }
        var bingshi = {};
        $.each($('#tab_bingshi-form-tab form ').serializeArray(), function(index, item) {
            if(item.name.indexOf('[]')>=0 && item.value !='') {
                let f = item.name.replace('[]', '')
                if(bingshi[f]) {
                    bingshi[f].push(item.value)
                }else{
                    bingshi[f] = [item.value]
                }
            }else{
                bingshi[item.name] = item.value;
            }
            
        });
        if(bingshi.zhusu == '') {
            $('.step-1').parents('.modal-body').find('.nav-tabs li a:eq(0)').tab('show');
            window.location.hash = 'tab_user-form-tab';
            $('#tab_bingshi-form-tab form ').submit()
            Dcat.swal.error('错误', '病例信息请填写完整');
            return false
        }
        console.log(bingshi)
        ajaxUrl('/api/saveBingshi', {bingshi, user}, function(res){
            Dcat.swal.success('提示', res.message).then((res)=>{
                Dcat.reload();
            })
            // $.admin.reload('刷新成功 !');
            return
        })
        
   })

JS
)
        
        ->button('<button class="btn btn-primary">初诊</button>');
        return str_replace('modal-xl', 'modal-1000', $modal->render());
    }

    public function bingshiForm(){
        Admin::translation('user-bingshi');
        return app()->make(UserBingshiController::class)->form(-1)->render();
    }
    public function userForm(){
        Admin::translation('user');
        return app()->make(UserController::class)->form(-1)->render();
    }

    public function nextStepHtml(){
        return '<div class="row" style="padding-bottom:50px;"><div class="col-lg-12 text-center"><button class="btn btn-primary step-1">上一步</button> <button class="btn btn-primary mx-1 step-2">下一步</button><button type="submit" class="btn btn-primary saveBingli">保存</button></div></div>';
    }

    public function view(){
        $tab = Tab::make();
        $tab->add('客户信息', $this->userForm() , true, 'user-form-tab');
        $tab->add('病例信息', $this->bingshiForm(), false, 'bingshi-form-tab');
        return $tab->render() . $this->nextStepHtml();
    }
}